//
//  TLCommentsAndLikesView.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/23.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLCommentsAndLikesView.h"
#import "TLTweetLikesView.h"
#import "TLCommentCell.h"
#import "TLMoreCommentCell.h"
#import "TLCommentsFrame.h"
#import "TLCommentFrame.h"
#import "TLTweet.h"
#import "TLComment.h"

@interface TLCommentsAndLikesView()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) UITableView *commentListView;

@property (nonatomic, weak) TLTweetLikesView *tweetLikesView;

@property (nonatomic, assign) NSInteger count;

@property (nonatomic, strong) NSMutableArray *commentsArray;

@end

@implementation TLCommentsAndLikesView

- (NSMutableArray *)commentsArray
{
    if(!_commentsArray){
        
        _commentsArray = [NSMutableArray array];
    }
    return _commentsArray;
}

- (void)setCommentsFrame:(TLCommentsFrame *)commentsFrame
{
    _commentsFrame = commentsFrame;
    
    self.frame = commentsFrame.frame;
    // 获取TLTweet模型数据
    TLTweet *tweet = commentsFrame.tweet;
    
    self.count = tweet.comments;
    
    self.tweetLikesView.commentCount = tweet.comments;
    self.tweetLikesView.frame = commentsFrame.likesFrame;
    self.tweetLikesView.likeUsers = tweet.like_users;

    if(tweet.comment_list.count > 0){
        
        self.commentListView.hidden = NO;
        self.commentsArray = [self commentFramesWithComments:tweet.comment_list];
        self.commentListView.frame = commentsFrame.commentsFrame;
        [self.commentListView reloadData];
    }else{
        
        self.commentListView.hidden = YES;
    }
}

/**
*  将TLComment模型数组转化成TLCommentFrame模型数组
*/
- (NSMutableArray *)commentFramesWithComments:(NSArray *)comments
{
    NSMutableArray *frames = [NSMutableArray array];
    for (TLComment *comment in comments) {
        TLCommentFrame *commentFrame = [[TLCommentFrame alloc] init];
        commentFrame.comment = comment;
        [frames addObject:commentFrame];
    }
    return frames;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        // 点赞头像列表
        TLTweetLikesView *tweetLikesView = [[TLTweetLikesView alloc] init];
        [self addSubview:tweetLikesView];
        self.tweetLikesView = tweetLikesView;

        // 添加评论列表控件
        UITableView *commentListView = [[UITableView alloc] init];
        commentListView.scrollEnabled = NO;
        commentListView.delegate = self;
        commentListView.dataSource = self;
        commentListView.separatorStyle = UITableViewCellSeparatorStyleNone;
        commentListView.backgroundColor = TLColor(243, 244, 246, 1.0);
        [self addSubview:commentListView];
        self.commentListView = commentListView;
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = self.commentsArray.count;
    if(self.count > 5){

        count = 6;
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell ;
    if(indexPath.row == 5){

        TLMoreCommentCell *moreCommentCell = [TLMoreCommentCell createMoreCommentCellWithTableView:tableView];
        moreCommentCell.count = self.count;
        cell = moreCommentCell;
    }else{
        
        TLCommentCell *commentCell = [TLCommentCell createCommentCellWithTableView:tableView];
        TLCommentFrame *commentFrame = self.commentsArray[indexPath.row];
        commentCell.commentFrame = commentFrame;
        if(self.count <= 5){
            commentCell.hideSplitline = (indexPath.row == self.commentsArray.count - 1);
        }else{
            commentCell.hideSplitline = NO;
        }
        cell = commentCell;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rowHeight;
    if(indexPath.row == 5){
        
        rowHeight = 40.0;
    }else{
        
        TLCommentFrame *commentFrame = self.commentsArray[indexPath.row];
        rowHeight = commentFrame.cellHeight;
    }
    return rowHeight;
}

@end
